//
//  PageShowView.m
//  MyShoes
//
//  Created by intel intel on 12-2-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PageShowView.h"
#import "GlobalVar.h"
#import "ImageButton.h"

@implementation PageShowView
@synthesize pics;
@synthesize currentIndex;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        btns = [[NSMutableArray alloc]init];
    }
    return self;
}

-(void)setPics:(NSArray *)_data{
	if (pics!= _data) {
		pics= _data;
	}else{
        return;
    }
    
    for (UIView *view in btns) {
        [view removeFromSuperview];
    }
    [btns removeAllObjects];
    
    currentIndex = -1;
    int space = 5;
    int i=0;
    
    for (NSString *pic in pics) {
       
        ImageButton *imageButton = [[[NSBundle mainBundle] loadNibNamed:@"ImageButton" owner:self options:nil]lastObject];
        [imageButton initView];
        NSString *imgname = [[NSString alloc] init];
        if ([pic isEqualToString:@"icon_ammeter"]||[pic isEqualToString:@"icon_camera"]||[pic isEqualToString:@"sur_0"]||[pic isEqualToString:@"sur_1"]||[pic isEqualToString:@"sur_2"])
        {
            imgname = [NSString stringWithFormat:@"%@",pic];
        }
        else
        {
            imgname = [NSString stringWithFormat:@"%@close",pic];
        }
        NSLog(@"%@",imgname);
        
        [imageButton.img setImage:[UIImage imageNamed:imgname]];
        imageButton.backgroundColor = [UIColor grayColor];
        imageButton.btn.tag = 100+i;
        [imageButton.btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        imageButton.frame = CGRectMake(space+i*(space+self.frame.size.height), 0, self.frame.size.height, self.frame.size.height);
        
        [self addSubview:imageButton];
        [btns addObject:imageButton];
        
        i++;
    }
    
    if (i>0) {
        [self setCurrentPage:0];
    }

}

-(void)btnClicked:(UIButton *)sender
{
    int tag = sender.tag - 100;
    [self setCurrentPage:tag];
}


-(void)setCurrentPage:(NSInteger)page
{
    if (currentIndex != -1) {
        ImageButton *imageButton = (ImageButton *)[btns objectAtIndex:currentIndex];
        [imageButton setSelected:NO];
    }
    
    currentIndex = page;
//    if (page >= 12) {
//        page = 11;
//    }
    ImageButton *imageButton = (ImageButton *)[btns objectAtIndex:page];
    [imageButton setSelected:YES];
    
}


@end
